<?php
/**
 * created by zzy
 * date: 2018/1/26 14:04
 */
namespace App\Eloquent\Ygt;

use Illuminate\Database\Eloquent\SoftDeletes;
class WasteBuyer extends DbEloquent{

    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $table    = 'ygt_waste_buyer';

    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'厂id','value'=>0,'is_must'=>0],
        ['name'=>'user_id','title'=>'用户id','value'=>0,'is_must'=>0],
        ['name'=>'title','title'=>'收品单位名称','value'=>'','is_must'=>1],
        ['name'=>'first_letter','title'=>'收品单位名称','value'=>'','is_must'=>0],
        ['name'=>'img_id','title'=>'头像','value'=>0,'is_must'=>0],
        ['name'=>'level','title'=>'收品单位评级','value'=>0,'is_must'=>0],
        ['name'=>'pay_type','title'=>'结算方式','value'=>'','is_must'=>0],
        ['name'=>'contact_name','title'=>'收品单位联系人','value'=>'','is_must'=>1],
        ['name'=>'contact_mobile','title'=>'联系人手机','value'=>'','is_must'=>1],
        ['name'=>'reg_mobile','title'=>'关联号码','value'=>'','is_must'=>0],
        ['name'=>'short_mobile','title'=>'虚拟网短号','value'=>'','is_must'=>0],
        ['name'=>'province_id','title'=>'收品单位地址','value'=>0,'is_must'=>0],
        ['name'=>'city_id','title'=>'收品单位地址','value'=>0,'is_must'=>0],
        ['name'=>'area_id','title'=>'收品单位地址','value'=>0,'is_must'=>0],
        ['name'=>'address','title'=>'收品单位地址','value'=>'','is_must'=>0],
        ['name'=>'province_name','title'=>'收品单位地址','value'=>'','is_must'=>0],
        ['name'=>'city_name','title'=>'收品单位地址','value'=>'','is_must'=>0],
        ['name'=>'area_name','title'=>'收品单位地址','value'=>'','is_must'=>0],
        ['name'=>'main_product','title'=>'主营产品','value'=>'','is_must'=>0],
        ['name'=>'add_user_id','title'=>'添加人','value'=>'','is_must'=>0],
    ];

    //根据type不同联合的表不同
    //默认不联合表
    public static function getJoinList($where,$limit='',$offset='',$type=1,$orderby=['ygt_waste_buyer.id','desc'])
    {
        switch($type)
        {
            case 1:
                $join = [
                    ['table'=>'ygt_img_upload','field_l'=>'ygt_img_upload.id','field_c'=>'=','field_r'=>'ygt_waste_buyer.img_id'],
                ];
                $column             = 'ygt_waste_buyer.*';
                $column             .= ',ygt_img_upload.img_url';
                break;
            case 2:
                $join = [
                    ['table'=>'ygt_company','field_l'=>'ygt_company.id','field_c'=>'=','field_r'=>'ygt_waste_buyer.company_id'],
                ];
                $column             = 'ygt_company.id,ygt_company.title,ygt_company.first_letter';
                break;
            default:
                $column             = 'ygt_waste_buyer.*';
                $join               = '';
        }
        $groupby            = '';
        $result             = self::getList($where,$column,$limit,$offset,$orderby,$groupby,$join);
        return $result;
    }
    //根据type不同联合的表不同
    //默认不联合表
    public static function getJoinInfo($where,$type=1,$orderby=['ygt_waste_buyer.id','desc'])
    {
        switch($type)
        {
            case 1:
                //取材料的流水列表
                $join = [
                    ['table'=>'ygt_img_upload','field_l'=>'ygt_img_upload.id','field_c'=>'=','field_r'=>'ygt_waste_buyer.img_id'],
                ];
                $column             = 'ygt_waste_buyer.*';
                $column             .= ',ygt_img_upload.img_url';
                break;
            default:
                $column             = 'ygt_waste_buyer.*';
                $join               = '';
        }
        $groupby            = '';
        $result             = self::getInfo($where,$column,$orderby,$groupby,$join);
        return $result;
    }

    public static function getLevelListByType($type=1)
    {
        $data           = [
            ['id'=>1,'title'=>'一级'],
            ['id'=>2,'title'=>'二级'],
            ['id'=>3,'title'=>'三级'],
        ];
        switch ($type)
        {
            case 1:
                $result     = $data;
                break;
            case 2:
                foreach($data as $key=>$val)
                {
                    $result[$val['id']] = $val['title'];
                }
                break;
            default:
                $result     = $data;
        }
        return $result;
    }
}
